package fw.asmx.launcher.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import fw.asmx.launcher.R;
import fw.asmx.launcher.adapter.item.AppItem;
import net.sourceforge.pinyin4j.PinyinHelper;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

import java.util.ArrayList;
import java.util.HashMap;

public class AlphabeticalOrderClassifyAppListAdapter extends BaseAdapter implements StickyListHeadersAdapter
{
    private int[] alphabeticalIndex = new int[27];
    private HashMap<Integer, ArrayList<AppItem>> map;

    private String apt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#";

    private Context context;
    private int resHead;
    private int resItem;

    public AlphabeticalOrderClassifyAppListAdapter(Context context, int resHead, int resItem)
    {
        super();
        this.context = context;
        this.resHead = resHead;
        this.resItem = resItem;
        for (int i = 0; i < 27; i++)
        {
            alphabeticalIndex[i] = i;
        }
        //
        map = new HashMap<>();
        for (int i = 0; i < 27; i++)
        {
            map.put(i, new ArrayList<AppItem>());
        }
    }

    private boolean isAlphabetical(char c)
    {
        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
        {
            return true;
        }
        return false;
    }

    public void addItem(String appName, AppItem ai)
    {
        int loc = 26;
        char fstChr = appName.toUpperCase().charAt(0);
        if (!Character.isDigit(fstChr))
        {
            if (isAlphabetical(fstChr))
            {
                loc = apt.indexOf(fstChr);
            }
            else
            {

                String[] py = PinyinHelper.toHanyuPinyinStringArray(fstChr);
                if (py != null)//是汉字
                {
                    loc = apt.indexOf(py[0].toUpperCase().charAt(0));
                }
            }
        }
        //
        map.get(loc).add(ai);
        for (int i = loc + 1; i < 27; i++)
        {
            alphabeticalIndex[i]++;
        }
    }

    public int getHeadPositionViaAlphabetical(String alphabetical)
    {
        int index = apt.indexOf(alphabetical);
        if (index == -1)
        {
            index = 26;
        }
        return alphabeticalIndex[index];
    }

    public int getItemPositionViaAlphabetical(String alphabetical)
    {
        int index = apt.indexOf(alphabetical);
        if (index == -1)
        {
            index = 26;
        }
        return alphabeticalIndex[index] + 1;
    }

    public int getTypeItemCount(String alphabetical)
    {
        int index = apt.indexOf(alphabetical);
        if (index == -1)
        {
            index = 26;
        }
        return map.get(index).size();
    }



    @Deprecated
    @Override
    public int getCount()
    {
        int ttl = 0;
        for (int i = 0; i < 27; i++)
        {
            ttl += map.get(i).size();
        }
        return ttl;
    }

    @Override
    public Object getItem(int position)
    {
        int cnt = 0;
        int curCnt;
        for (int i = 0; i < 27; i++)
        {
            curCnt = map.get(i).size();
            cnt += curCnt;
            if (cnt > position)
            {
                return map.get(i).get(curCnt - (cnt - position));
            }
            else if (cnt == position)
            {
                for(++i;i<27;i++)
                {
                    if(map.get(i).size()!=0)
                    {
                        return map.get(i).get(0);
                    }
                }
            }
        }
        return null;
    }

    @Deprecated
    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override public View getHeaderView(int pos, View cv, ViewGroup vg)
    {
        Button bt;

        if (cv == null)
        {
            bt=(Button)LayoutInflater.from(context).inflate(resHead,null);
        }
        else
        {
            bt = (Button) cv;
        }

        int cnt = 0;
        int curCnt;
        for (int i = 0; i < 27; i++)
        {
            curCnt = map.get(i).size();
            cnt += curCnt;
            if (cnt > pos)
            {
                bt.setText(apt.substring(i,i+1));
                break;
            }
            else if (cnt == pos)
            {
                bt.setText(apt.substring(i+1,i+2));
                break;
            }
        }
        return bt;
    }

    @Override public long getHeaderId(int pos)
    {
        int cnt = 0;
        int curCnt;
        for (int i = 0; i < 27; i++)
        {
            curCnt = map.get(i).size();
            cnt += curCnt;
            if (cnt > pos)
            {
                return apt.charAt(i);
            }
            else if (cnt == pos)
            {
                return apt.charAt(i+1);
            }
        }
        return "$".charAt(0);
    }

    class ViewHolder
    {
        ImageView iv;
        TextView tv;
    }

    @Override
    public View getView(int pos, View cv, ViewGroup parent)
    {
        LinearLayout ll;
        ViewHolder vh;

        //item
        if (cv == null)
        {
            vh = new ViewHolder();
            ll = new LinearLayout(context);
            LayoutInflater.from(context).inflate(resItem, ll, true);
            vh.iv = (ImageView) ll.findViewById(R.id.iv_app_icon);
            vh.tv = (TextView) ll.findViewById(R.id.tv_app_name);
            ll.setTag(vh);
        }
        else
        {
            ll = (LinearLayout) cv;
            vh = (ViewHolder) ll.getTag();
        }

        vh.tv.setBackgroundColor(Color.parseColor("#23232323"));

        AppItem ai = (AppItem) getItem(pos);
        vh.iv.setImageDrawable(ai.getIcon());
        vh.tv.setText(ai.getAppName());
        return ll;
    }
}
